package com.hemerasolutions.v2trainer.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.hemerasolutions.v2trainer.component.interfaces.list.StepListLevelsInterface;
import com.hemerasolutions.v2trainer.component.interfaces.search.TrainingSystemSearchCompInterface;
import com.hemerasolutions.v2trainer.controller.interfaces.TrainingSystemBeanInterface;
import com.hemerasolutions.v2trainer.delegate.interfaces.TrainingSystemSearchDelegateInterface;
import com.hemerasolutions.v2trainer.model.step.Step;
import com.hemerasolutions.v2trainer.model.training.Training;
import com.hemerasolutions.v2trainer.model.training.TrainingStep;
import com.hemerasolutions.v2trainer.service.ExceptionService;
import com.hemerasolutions.v2trainer.service.interfaces.TrainingSystemServiceInterface;

@Controller
@ManagedBean(name="trainingSystemBean")
@Scope("view")
public class TrainingSystemBean extends BaseBean implements TrainingSystemBeanInterface, TrainingSystemSearchDelegateInterface  {

	private static final long serialVersionUID = 1L;

	private List<Training> trainingSystems;
	
	private Training selTrainingSystem;
	
	private Training trainingSystem;

	@Autowired
	private TrainingSystemSearchCompInterface trainingSystemSearchComp;
	
	@Autowired
	private TrainingSystemServiceInterface trainingSystemService;
	
	@Autowired
	private StepListLevelsInterface levelsHandler;


	public TrainingSystemBean() {
		super();
		
	}

	@PostConstruct
	public void init(){
		this.trainingSystem = null;
		this.listAll();
		trainingSystemSearchComp.setTsSearchDelegate(this);
		trainingSystemSearchComp.refresh();
		
	}
	
	
	private void listAll() {
	   	Map<String,Object> fields = new HashMap<String, Object>();
    	
    		fields.put("librarys",this.getActiveUserAllowedLibrarys());
    		fields.put("trainingSystem",true);

			
		    this.trainingSystems = this.trainingSystemService.searchTrainingSystems(fields);		
		
	}

	public void clear(){
		this.trainingSystem = null;
	}

	public void toViewTrainingSystem(){
		for(TrainingStep ts :this.trainingSystem.getTrainingSteps()){
			this.collapseAll(ts.getRootStep().getSteps());
		}

		this.levelsHandler.setStepLevels(this.trainingSystem.getTrainingSteps());
		this.levelsHandler.refresh();
		
		
	}
	
	private void collapseAll(List<Step> steps) {
		for(Step step: steps){
			if(step.getType() > Step.REPETITION_STEP){
				this.collapseAll(step.getSteps());
			}else{
				step.setMark(true);
			}
		}
		
	}

	public void createTrainingSystem(){
		try {
			this.trainingSystem.setCreatedAt(new Date());
			this.trainingSystem.setChangedBy(this.getActiveUser());
			this.trainingSystem.setLastChange(new Date());
			trainingSystemService.createTrainingSystem(trainingSystem);
			this.showInfoMessage(	this.localizedText("CreatedWithSuccess"),
					this.localizedText("TheTrainingSystem") + " " +
					this.trainingSystem.getName() + "\n , " + 
					this.localizedText("wasCreatedWithSuccess"));			
			this.trainingSystem = null;
			this.listAll();
		} catch (ExceptionService e) {
			this.showErrorMessage(this.localizedText("CreateError"), e.getLocalizedMessage());
			e.printStackTrace();
		}
	}
	
	
	public void saveTrainingSystem(){
		try {
			trainingSystemService.saveTrainingSystem(trainingSystem);
			this.trainingSystem.setChangedBy(this.getActiveUser());
			this.trainingSystem.setLastChange(new Date());
			this.showInfoMessage(	this.localizedText("SavedWithSuccess"),
					this.localizedText("TheTrainingSystem") + " " +
					this.trainingSystem.getName() + "\n , " +
					this.localizedText("wasSavedWithSuccess"));			
			this.trainingSystem = null;
			this.listAll();
			
		} catch (ExceptionService e) {
			this.showErrorMessage(this.localizedText("SaveError"), e.getLocalizedMessage());
		}
	}
	
	public void cancelChange(){
		if(this.trainingSystem.getId() != 0){
			this.trainingSystem = trainingSystemService.readTrainingSystem(trainingSystem);
		}else{
			this.trainingSystem = new Training();
		}
	}	
	
	public void deleteTrainingSystem(){
		
		try{
			trainingSystemService.deleteTrainingSystem(trainingSystem);
			this.showInfoMessage(	this.localizedText("Deleted"),
									this.localizedText("TrainingSystemDeletedWithSuccess"));
			this.trainingSystems.remove(this.trainingSystem);
			this.trainingSystem = null;
		}catch(ExceptionService e){
			this.showErrorMessage(this.localizedText("DeleteError"),
					e.getLocalizedMessage());
		}
		
	}
	
	
	public void toAddTrainingSystem(){
		this.trainingSystem  = new Training();
		this.trainingSystem.setTrainingSystem(true);
		this.trainingSystem.prepareLevels();

		this.levelsHandler.setStepLevels(this.trainingSystem.getTrainingSteps());
		this.levelsHandler.refresh();
	
	}	
	
	
	public List<Training> getTrainingSystems() {
		return trainingSystems;
	}

	public void setTrainingSystems(List<Training> trainingSystems) {
		this.trainingSystems = trainingSystems;
	}


	public TrainingSystemServiceInterface getTrainingSystemService() {
		return trainingSystemService;
	}

	public void setTrainingSystemService(TrainingSystemServiceInterface trainingSystemService) {
		this.trainingSystemService = trainingSystemService;
	}

	public Training getSelTrainingSystem() {
		return selTrainingSystem;
	}

	public void setSelTrainingSystem(Training selTrainingSystem) {
		this.selTrainingSystem = selTrainingSystem;
	}

	public Training getTrainingSystem() {
		return trainingSystem;
	}

	public void setTrainingSystem(Training trainingSystem) {
		this.trainingSystem = trainingSystem;
	}

	public StepListLevelsInterface getLevelsHandler() {
		return levelsHandler;
	}

	public void setLevelsHandler(StepListLevelsInterface levelsHandler) {
		this.levelsHandler = levelsHandler;
	}

	public TrainingSystemSearchCompInterface getTrainingSystemSearchComp() {
		return trainingSystemSearchComp;
	}

	public void setTrainingSystemSearchComp(
			TrainingSystemSearchCompInterface trainingSystemSearchComp) {
		this.trainingSystemSearchComp = trainingSystemSearchComp;
	}

	@Override
	public boolean onTrainingSystemAdd(Training trainingSystem) {
		return false;
	}

	@Override
	public void onTrainingSystemImportSteps(List<Step> steps) {
	
	}

	@Override
	public void onSearchComplete(List<Training> trainingSystems) {
		this.trainingSystems = new ArrayList<Training>();
		this.trainingSystems.addAll(trainingSystems);		
	}

	
}
